import { _decorator, Node, Label, view, UITransform, find } from 'cc';
import { App } from '../../App';
import UtilTool from '../../tools/UtilTool';
import ScrollHelper from '../ScrollHelper';
import AbstractComponent from '../../base/AbstractComponent';
import { MultiMsgMgr } from './../multiplay/MultiMsgMgr';
const { ccclass, property } = _decorator;

let PAGE_DEFAULT_SIZE = 30

// 球室持有人收益记录
@ccclass('IncomeRecord')
export class IncomeRecord extends AbstractComponent {
    @property(Node)
    contentNode: Node = null;

    @property(Node)
    scrollNode: Node = null;

    @property(Node)
    emptyNode : Node = null;

    private itemNum = 5
    // [listData]列表数据  [isLoading]是否加载中 [loadOver]是否加载结束(如果没有新的数据则认为是加载结束) [currPage]当前已经请求的第几页的数据 [dataDirty]是否需要重新拉取数据 
    // 记录数据
    private configData = {listData : [], isLoading : false, loadOver : false, currPage : 0, dataDirty : true, pageSize : PAGE_DEFAULT_SIZE}

    onLoad() {
        UtilTool.setScreen(this.node)
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())
    }

    start() {
        this.emptyNode.active = false
        this.reqData(true)
    }

    // 发送测试数据
    sendGetTestData(page, pageSize) : Promise<any> {

        return new Promise((resolve, reject) => {

            let data = {
                list : []
            }

            if(page >= 3) {
                resolve(data) // 大于等于3页就不再请求数据（即一共只有30个）
                return
            }

            for(let i = 0; i < pageSize; i++) {
                let index = page * pageSize + i + 1
                let item = {
                    gold : 2000,
                    createTime :120102100,
                    humanId : "0xadfffsdf",
                    gameType : 0,
                }
                data.list.push(item)
            }

            resolve(data)

        })

    }

    /**
     * 请求数据
     */
    reqData(pInit=true) {
        this.configData.isLoading = true
        
        UtilTool.printLog("test reqData", this.configData.currPage)

        MultiMsgMgr.getInstance().sendGetRoomIncome(this.configData.currPage, this.configData.pageSize)
        // this.sendGetTestData(this.configData.currPage, PAGE_DEFAULT_SIZE) // 测试数据
        .then(res =>{
            this.configData.currPage = this.configData.currPage + 1
            this.configData.isLoading = false
            
            res.list.forEach(element => {
                this.configData.listData.push(element)
            });
            if (res.list==null || res.list.length < this.configData.pageSize) {
                this.configData.loadOver = true
            }

            // 最新请求的数据列表大于0，才会去刷新界面
            if (res.list.length>0) {
                let param = {
                    init:pInit, 
                    data:this.configData.listData, 
                    itemNum:this.itemNum, 
                    itemFun: (obj,idx,objIdx)=>{
                        this.refreshItem(obj,idx,objIdx)
                    },
                    endFun:()=>{
                        this.OnScrollEnd()
                    }
                }

                this.scrollNode.getComponent(ScrollHelper).setData(param)
            }

            this.emptyNode.active = this.configData.listData.length <= 0

        })
    }

    /** 
     * 刷新数据
     * param obj item对象
     * param idx 实际的下标索引
     * param objIdx item本身的下标索引
    */
    refreshItem(obj,idx,objIdx) {
        if (this.configData.listData.length <= 0) {
            return
        }

        UtilTool.printLog("test refreshItem", idx, objIdx, this.configData.listData)

        let data = this.configData.listData[idx]
        let label_income = find("label_income", obj).getComponent(Label)
        let label_gameType = find("label_gameType", obj).getComponent(Label)
        let label_date = find("label_date", obj).getComponent(Label)

        label_income.string = UtilTool.formatStr(App.language.getLanguageLab(2051), data.gold) // 收益
        label_gameType.string = data.gameType == 0 ? App.language.getLanguageLab(116) : App.language.getLanguageLab(117) // 游戏类型

        let date = UtilTool.getTimeData(data.createTime)
        label_date.string = UtilTool.formatStr("%s-%s-%s %s:%s:%s", date.year, date.month, date.day, date.hour, date.min, date.second) // 时间

    }

    // 滚动视图滑到底部回调
    OnScrollEnd() {
        if (this.configData.isLoading) {
            return
        }
        if (this.configData.loadOver) {
            return
        }

        this.reqData(false)
    }

}

